var EventCollectorEvent = function (deltaTime, eventType, domObject, eventData)
{
    this.DeltaTime = deltaTime;
    this.EventType = eventType;
    this.DomObject = domObject;
    this.EventData = eventData;

    this.Serialize = function ()
    {
        return this.DeltaTime +
            String.fromCharCode(1) +
            this.EventType +
            String.fromCharCode(1) +
            (this.DomObject ? this.DomObject.id : "") +
            String.fromCharCode(1) +
            (this.EventData ? this.EventData.replace ? this.EventData.replace(/\s/g, "|||") : this.EventData : "");
    };

    this.Restore = function (serializationData)
    {
        var serializedData = serializationData.split(String.fromCharCode(1));
        this.DeltaTime = parseInt(serializedData[0]);
        this.EventType = serializedData[1];
        this.DomObject = serializedData[2] != "" ? document.getElementById(serializedData[2]) : null;
        this.EventData = serializedData[3];
    };

    this.Replay = function (speed)
    {
        try
        {
            var replayScript = "";
            switch (this.EventType)
            {
                case EventCollector.MOUSE_POSITION:
                    var mouseXY = this.EventData.split(",");
                    replayScript = "showMouse(" + mouseXY[0] + "," + mouseXY[1] + ");";
                    break;
                case EventCollector.MOUSE_OVER:
                    replayScript = "document.getElementById(\"" + this.DomObject.id + "\").onmouseover();";
                    break;
                case EventCollector.MOUSE_OUT:
                    replayScript = "document.getElementById(\"" + this.DomObject.id + "\").onmouseout();";
                    break;
                case EventCollector.TEXT_CHANGE:
                    replayScript = "var obj = document.getElementById(\"" + this.DomObject.id + "\"); if(obj.innerHTML) obj.innerHTML = \"" + this.EventData.replace(/\|\|\|/g, " ") + "\"; else obj.value = \"" + this.EventData.replace(/\|\|\|/g, " ") + "\"";
                    break;
                case EventCollector.RADIO_CLICK:
                    replayScript = "document.getElementById(\"" + this.DomObject.id + "\").checked = true;";
                    break;
                case EventCollector.CHECKBOX_CLICK:
                    replayScript = "document.getElementById(\"" + this.DomObject.id + "\").checked = " + (this.EventData == "true" ? "true" : "false") + ";";
                    break;
                case EventCollector.SCROLL_CHANGE_X:
                    var y = (document.all) ? document.body.scrollTop : window.pageYOffset;
                    window.scrollTo(parseInt(this.EventData), y);
                    break;
                case EventCollector.SCROLL_CHANGE_Y:
                    var x = (document.all) ? document.body.scrollLeft : window.pageXOffset;
                    window.scrollTo(x, parseInt(this.EventData));
                    break;
            }
            window.setTimeout(replayScript, this.DeltaTime * speed);
        }
        catch (e) { }
    }
};

var EventCollector =
{
    MOUSE_POSITION: "1",
    MOUSE_OVER: "2",
    MOUSE_OUT: "3",
    MOUSE_CLICK: "4",
    TEXT_CHANGE: "5",
    CHECKBOX_CLICK: "6",
    RADIO_CLICK: "7",
    SCROLL_CHANGE_X: "8",
    SCROLL_CHANGE_Y: "9",

    _Registry: new Array(),

    _StartDate: 0,

    MonitoringIndex: 0,

    Initialize: function ()
    {
        EventCollector._StartDate = (new Date()).valueOf();
    },

    RegisterEvent: function (eventType, domObject, eventData)
    {
        EventCollector._Registry.push(new EventCollectorEvent((new Date()).valueOf() - EventCollector._StartDate, eventType, domObject, eventData));
    },

    Clear: function ()
    {
        EventCollector._Registry.length = 0;
        EventCollector._StartDate = (new Date()).valueOf();
    },

    Serialize: function ()
    {
        var serializationData = "";
        for (var i = 0; i < EventCollector._Registry.length; i++)
        {
            serializationData += EventCollector._Registry[i].Serialize() + String.fromCharCode(2);
        }
        return serializationData.substr(0, serializationData.length - 1);
    },

    Replay: function (eventCollectorData, speed)
    {
        if (!speed) speed = 1;
        var eventCollectorEvents = eventCollectorData.split(String.fromCharCode(2));
        for (var i = 0; i < eventCollectorEvents.length; i++)
        {
            var evt = new EventCollectorEvent();
            evt.Restore(eventCollectorEvents[i]);
            evt.Replay(speed);
        }
    },

    PostDelayed: false,

    DelayedPost: function()
    {
        ServerRequest.Execute('/webcati/admin/monitoringDataCollector.asp', _Constants.Method.Post, Array('EVENT_DATA', EventCollector.Serialize()), false); EventCollector.Clear();

        if (EventCollector.PostDelayed == false)
        {
            EventCollector.PostDelayed = true;
            window.setTimeout("document.forms[0].submit();", 750);
            return false;
        }
        else
        {
            return true;
        }
    }
}
